﻿namespace ACCOUNTING.LVY.Business
{
    using ACCOUNTING;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Data;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;

    public class clsSoChungTu
    {
        private int mChieuDai = 10;
        private string mHauTo = "";
        private string mLoai = "";
        private string mLoaiChungTu = "";
        private double mSoChungTu;
        private string mTienTo = "";

        public string CreateVoucher(string pNamThang, int pLoai = 1)
        {
            string str = "";
            IDataParameter[] commandParameters = new IDataParameter[] { DBHelper.createParameter("@LoaiChungTu", DbType.Int64, ParameterDirection.Input, this.mLoaiChungTu), DBHelper.createParameter("@NamThang", DbType.Int64, ParameterDirection.Input, pNamThang), DBHelper.createParameter("@SoChungTu", DbType.Date, ParameterDirection.Output, 0) };
            DBHelper.ExecuteNonQuery(modGeneral.ConnectionString, CommandType.StoredProcedure, "Sp_CapSoChungTu_New", commandParameters);
            this.mSoChungTu = Conversions.ToDouble(commandParameters[2].Value);
            string str2 = this.TinhHauTo(this.mLoai, pNamThang);
            switch (pLoai)
            {
                case 0:
                    str = "000000000000000" + commandParameters[2].Value.ToString() + str2;
                    str = Strings.Trim(this.mTienTo) + Strings.Right(Strings.Trim(str), this.mChieuDai - Strings.Trim(this.mTienTo).Length);
                    break;

                case 1:
                    str = "000000000000000" + commandParameters[2].Value.ToString();
                    str = Strings.Trim(this.mTienTo) + str2 + "/" + Strings.Right(Strings.Trim(str), this.mChieuDai - (this.mTienTo.Trim() + str2).Trim().Length);
                    break;
            }
            return str.ToString();
        }

        public bool Load(string pLoaiChungTu)
        {
            bool flag=false;
            IDataParameter[] commandParameters = new IDataParameter[2];
            try
            {
                string commandText = "SELECT * FROM HT_SoChungTu WHERE Rtrim(LoaiChungTu) = '" + pLoaiChungTu.ToString() + "'";
                IDataReader dataReader = DBHelper.ExecuteReader(modGeneral.ConnectionString, CommandType.Text, commandText, commandParameters);
                if (dataReader.Read())
                {
                    this.mLoaiChungTu = Conversions.ToString(modGeneral.GetValue(dataReader, 0, modGeneral.typeOfColumn.GetString));
                    this.mTienTo = Conversions.ToString(modGeneral.GetValue(dataReader, 1, modGeneral.typeOfColumn.GetString));
                    this.mHauTo = Conversions.ToString(modGeneral.GetValue(dataReader, 2, modGeneral.typeOfColumn.GetString));
                    this.mLoai = Conversions.ToString(modGeneral.GetValue(dataReader, 3, modGeneral.typeOfColumn.GetString));
                    this.mChieuDai = Conversions.ToInteger(modGeneral.GetValue(dataReader, 4, modGeneral.typeOfColumn.GetInt32));
                }
                dataReader.Close();
                flag = true;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public DataTable LoadChungTu(string strLoai, string strNamThang, string strCondition = "")
        {
            DataTable table;
            string commandText = "";
            string str3 = "";
            string str = "";
            if (strLoai == "")
            {
                strLoai = "M";
            }
            try
            {
                switch (strLoai)
                {
                    case "M":
                        str = ", TG.SoChungTuThang ";
                        str3 = " And TG.Thang = '" + strNamThang + "'";
                        break;

                    case "Q":
                        str = ", TG.SoChungTuQuy ";
                        str3 = " And TG.Quy = '" + LVYModule.Month2Quarter(strNamThang) + "'";
                        break;

                    case "Y":
                        str = ", TG.SoChungTuNam ";
                        str3 = " And TG.Nam = '" + LVYModule.sLeft(strNamThang, 4) + "'";
                        break;
                }
                commandText = " SELECT CT.LoaiChungTu, CT.TienTo, CT.ChieuDai, CT.DienGiai " + str + "as SoChungTu FROM HT_SoChungTu CT LEFT join HT_CapSoChungTu_New TG on CT.LoaiChungTu = TG.LoaiChungTu " + str3;
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public string TinhHauTo(string strLoai, string pNamThang)
        {
            string str = "";
            int num = Conversions.ToInteger(LVYModule.sRight(pNamThang, 2));
            switch (strLoai)
            {
                case "Y":
                    return ("-" + LVYModule.sMid(pNamThang, 3, 2));

                case "H":
                    if (num <= 6)
                    {
                        return ("-01" + LVYModule.sMid(pNamThang, 3, 2));
                    }
                    return ("-02" + LVYModule.sMid(pNamThang, 3, 2));

                case "Q":
                    if ((num >= 1) & (num <= 3))
                    {
                        str = "-01" + LVYModule.sMid(pNamThang, 3, 2);
                    }
                    if ((num >= 4) & (num <= 6))
                    {
                        str = "-02" + LVYModule.sMid(pNamThang, 3, 2);
                    }
                    if ((num >= 7) & (num <= 9))
                    {
                        str = "-03" + LVYModule.sMid(pNamThang, 3, 2);
                    }
                    if ((num >= 10) & (num <= 12))
                    {
                        str = "-04" + LVYModule.sMid(pNamThang, 3, 2);
                    }
                    return str;

                case "M":
                    return ("-" + LVYModule.sRight("00" + Conversions.ToString(num), 2) + LVYModule.sMid(pNamThang, 3, 2));
            }
            return str;
        }

        public bool UndoVoucher(string pNamThang)
        {
            IDataParameter[] commandParameters = new IDataParameter[] { DBHelper.createParameter("@LoaiChungTu", DbType.Int64, ParameterDirection.Input, this.mLoaiChungTu), DBHelper.createParameter("@NamThang", DbType.Int64, ParameterDirection.Input, pNamThang), DBHelper.createParameter("@SoChungTu", DbType.Date, ParameterDirection.Input, this.mSoChungTu), DBHelper.createParameter("@pError", DbType.Double, ParameterDirection.Output, 0) };
            DBHelper.ExecuteNonQuery(modGeneral.ConnectionString, CommandType.StoredProcedure, "Sp_UndoSoChungTu_New", commandParameters);
            return Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectEqual(commandParameters[3].Value, 0, false);
        }

        public string LoaiChungTu
        {
            get
            {
                return this.mLoaiChungTu;
            }
            set
            {
                this.mLoaiChungTu = value;
            }
        }

        public double SoChungTu
        {
            get
            {
                return this.mSoChungTu;
            }
            set
            {
                this.mSoChungTu = value;
            }
        }

        public string TienTo
        {
            get
            {
                return this.mTienTo;
            }
            set
            {
                this.mTienTo = value;
            }
        }
    }
}

